package com.brillilab.starter.controller.aio.plans;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.util.Assert;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.brillilab.common.entity.ResponseVo;
import com.brillilab.common.utils.DateUtil;
import com.brillilab.domain.enums.account.IntegralTypeEnum;
import com.brillilab.domain.po.account.IntegralRecord;
import com.brillilab.domain.vo.account.IntegralAddTipsVo;
import com.brillilab.domain.vo.experiments.ExperimentStartVo;
import com.brillilab.domain.vo.lab.MemberInfoVo;
import com.brillilab.domain.vo.plans.PlanExperimentAddVo;
import com.brillilab.domain.vo.plans.PlanMemberRoleVo;
import com.brillilab.domain.vo.plans.PlanTargetAddVo;
import com.brillilab.domain.vo.plans.PlanVo;
import com.brillilab.domain.vo.user.UserInfoVo;
import com.brillilab.service.core.plans.IPlanService;
import com.brillilab.service.logic.account.UserAccountLogic;
import com.brillilab.service.logic.plans.PlanLogic;
import com.brillilab.service.logic.user.UsersLogic;
import com.brillilab.starter.constant.StarterConstant;

import cn.hutool.core.collection.CollUtil;

/**
 * 工作计划表
 *
 * @author LJ 2019-06-26
 */
@RestController
@RequestMapping("/api/plan")
public class PlanController {

	@Resource
	private UsersLogic usersLogic;
	@Resource
	private PlanLogic planLogic;
	@Resource
	private IPlanService planService;
	@Resource
	private UserAccountLogic userAccountLogic;

	/**
	 * 计划列表颜色值
	 * 
	 * @return
	 */
	@GetMapping("/colorList")
	public ResponseVo colorList() {
		List<String> colorList = CollUtil.toList("#EB5444", "#F6AC2D", "#00CB7C","#70AACE", "#797BDA", "#B763A1", "#BAA753");
		return ResponseVo.success(colorList);
	}

	/**
	 * 获取登录人对应计划成员角色
	 * 
	 * @return
	 */
	@GetMapping("/role")
	public ResponseVo getMemberRole(@RequestParam Long labMemberId,
			@RequestHeader(name = StarterConstant.TOKEN) String token) {
		UserInfoVo userInfo = usersLogic.getUserByToken(token);
		PlanMemberRoleVo vo = planLogic.getMemberRole(userInfo, labMemberId);
		return ResponseVo.success(vo);
	}

	/**
	 * 获取成员筛选列表
	 * 
	 * @return
	 */
	@GetMapping("/memberList")
	public ResponseVo planMemberScreenList(@RequestHeader(name = StarterConstant.TOKEN) String token) {
		UserInfoVo userInfo = usersLogic.getUserByToken(token);
		List<MemberInfoVo> memberList = planLogic.getPlanMemberScreenList(userInfo.getLabId(),
				userInfo.getLabMemberId(), userInfo.getIsOwner());
		return ResponseVo.success(memberList);
	}

	/**
	 * 根据时间段获取计划列表
	 */
	@GetMapping("/list")
	public ResponseVo getPlanList(@RequestHeader(name = StarterConstant.TOKEN) String token,
			@RequestParam Long labMemberId, @RequestParam String startDate, @RequestParam String endDate) {
		List<PlanVo> voList = planService.getPlanVoList(labMemberId, startDate, endDate);
		return ResponseVo.success(voList);
	}

	/**
	 * 添加计划实验流组
	 * 
	 * @return
	 */
	@PostMapping("/targetAdd")
	public ResponseVo planTargetAdd(@RequestBody PlanTargetAddVo addVo,
			@RequestHeader(name = StarterConstant.TOKEN) String token) {
		Assert.isTrue(!CollectionUtils.isEmpty(addVo.getTopicExperimentList()), "请选择实验目标");
//		Assert.notNull(addVo.getLabMemberId(),"请选择实验计划人");
		UserInfoVo userInfo = usersLogic.getUserByToken(token);
		if (addVo.getPlanTime() != null) {
			String newDateStr = DateUtil.dateString(new Date(), "yyyy-MM-dd HH");
			Date newDate = DateUtil.stringToDate(newDateStr, "yyyy-MM-dd HH");
			Assert.isTrue(addVo.getPlanTime().getTime() >= newDate.getTime(), "计划时间必须大于当前时间");
		}
		if (addVo.getLabMemberId() == null) {
			addVo.setLabMemberId(userInfo.getLabMemberId());
		}
		addVo.setLabId(userInfo.getLabId());
		PlanVo vo = planLogic.planTargetAdd(userInfo, addVo);

		IntegralAddTipsVo tipsVo = new IntegralAddTipsVo();
		if (vo != null) {
			IntegralRecord integralRecord = userAccountLogic.addIntegral(userInfo, IntegralTypeEnum.PLAN, vo.getId());
			if (integralRecord != null) {
				tipsVo.setTntegralTips("恭喜您，完成新建计划");
			}
			vo.setTipsVo(tipsVo);
		}
		return ResponseVo.success(vo);
	}

	/**
	 * 添加单个实验目标到计划时间
	 * 
	 * @return
	 */
	@PostMapping("/add")
	public ResponseVo planExperimentAdd(@RequestBody PlanExperimentAddVo addVo,
			@RequestHeader(name = StarterConstant.TOKEN) String token) {
		UserInfoVo userInfo = usersLogic.getUserByToken(token);
		if (addVo.getLabMemberId() == null) {
			addVo.setLabMemberId(userInfo.getLabMemberId());
		}
		addVo.setLabId(userInfo.getLabId());
		PlanVo vo = planLogic.planExperimentAdd(userInfo, addVo);

		IntegralAddTipsVo tipsVo = new IntegralAddTipsVo();
		if (vo != null) {
			IntegralRecord integralRecord = userAccountLogic.addIntegral(userInfo, IntegralTypeEnum.PLAN, vo.getId());
			if (integralRecord != null) {
				tipsVo.setTntegralTips("恭喜您，完成新建计划");
			}
		}
		vo.setTipsVo(tipsVo);
		return ResponseVo.success(vo);
	}

	/**
	 * 移除实验目标的计划时间
	 * 
	 * @return
	 */
	@GetMapping("/delete")
	public ResponseVo planDelete(@RequestParam Long planId, @RequestHeader(name = StarterConstant.TOKEN) String token) {
		UserInfoVo userInfo = usersLogic.getUserByToken(token);
		planLogic.deletePlan(planId, userInfo);
		return ResponseVo.success();
	}

	/**
	 * 移除实验流组中实验目标
	 * 
	 * @return
	 */
	@GetMapping("/experimentDelete")
	public ResponseVo experimentDelete(@RequestParam Long experimentGroupId,
			@RequestHeader(name = StarterConstant.TOKEN) String token) {
		UserInfoVo userInfo = usersLogic.getUserByToken(token);
		planLogic.experimentDelete(userInfo, experimentGroupId);
		return ResponseVo.success();
	}

	/**
	 * 添加实验目标到实验流组中
	 * 
	 * @return
	 */
	@GetMapping("/experimentGroupAdd")
	public ResponseVo experimentGroupAdd(@RequestParam Long topicExperimentId, @RequestParam Long targetGroupId,
			@RequestHeader(name = StarterConstant.TOKEN) String token) {
		planLogic.experimentGroupAdd(targetGroupId, topicExperimentId);
		return ResponseVo.success();
	}

	/**
	 * 点击实验开始/继续/重复按钮时先调用，再进入实验中
	 * 
	 * @return
	 */
	@GetMapping("/experimentStart")
	public ResponseVo experimentStart(@RequestParam Long topicExperimentId,
			@RequestHeader(name = StarterConstant.TOKEN) String token) {
		UserInfoVo userInfo = usersLogic.getUserByToken(token);
		ExperimentStartVo startVo = planLogic.experimentStart(topicExperimentId, userInfo);
		return ResponseVo.success(startVo);
	}

}
